<?php

namespace App\Http\Controllers\SystemManagement;

use App\Models\Constant\Fields;
use App\Models\SystemManagement\WorkSpace;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * Class WorkspaceController
 * @package App\Http\Controllers\SystemManagement
 * @group 工作区域管理
 */
class WorkspaceController extends Controller
{
    /**
     * 工作区域列表
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
     *
     * @queryParam id
     * @queryParam name
     * @queryParam area
     *
     * @response 200
     */
    public function index()
    {
        $data = WorkSpace::get();
        return response()->json(encodeMsg($data,200));
    }

    /**
     * 新增工作区域
     * Store a newly created resource in storage.
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     *
     * @bodyParam name string required 作业区域
     * @bodyParam times string required 作业时间
     * @bodyParam area string required 作业面积
     *
     * @response 201
     * @response 400
     */
    public function store(\App\Http\Requests\System\Workspace $request)
    {
        $req = $request->input();
        try{
            WorkSpace::create($req);
            return response()->json(encodeMsg("新增成功",201));
        }catch (\Exception $e){
            //dd($e->getMessage());
            return response()->json(encodeMsg("新增失败",400));
        }
    }

    /**
     * 查看作业区域
     * Display the specified resource.
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *
     * @bodyParam id int required the workspace id.
     *
     * @queryParam id int 作业id
     * @queryParam name string 作业区域
     * @queryParam times string 作业时间
     * @queryParam area string 作业面积
     *
     * @response 200
     * @response 404
     */
    public function show($id)
    {
        $data = WorkSpace::find($id);
        if(is_null($data)){
            return response()->json(encodeMsg("该记录不存在",404));
        }
        return response()->json(encodeMsg($data,200));
    }

    /**
     * 修改作业区域
     * Update the specified resource in storage.
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *
     * @bodyParam name string required 作业区域
     * @bodyParam times string required 作业时间
     * @bodyParam area string required 作业面积
     *
     * @response 200
     * @response 404
     */
    public function update(\App\Http\Requests\System\Workspace $request, $id)
    {
        $data = WorkSpace::find($id);
        if(is_null($data)){
            return response()->json(encodeMsg("该记录不存在",404));
        }
        try{
            WorkSpace::where(Fields::ID,$id)->update($request->input());
            return response()->json(encodeMsg("修改成功",205));
        }catch (\Exception $e){
            //dd($e->getMessage());
            return response()->json(encodeMsg("修改失败",400));
        }
    }

    /**
     * 单条删除数据
     * Remove the specified resource from storage.
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *
     * @bodyParam id int required the workspace id.
     *
     * @response 204
     * @response 404
     */
    public function destroy($id)
    {
        $data = WorkSpace::find($id);
        if(is_null($data)){
            return response()->json(encodeMsg("该记录不存在",404));
        }
        try{
            $data->delete();
            return response()->json(encodeMsg("删除成功",204));
        }catch (\Exception $e){
            //dd($e->getMessage());
            return response()->json(encodeMsg("删除失败",400));
        }
    }
}
